package org.mango.callback;

import com.baidu.unbiz.fluentvalidator.DefaultValidateCallback;
import com.baidu.unbiz.fluentvalidator.ValidateCallback;
import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.validator.element.ValidatorElementList;
import org.apache.log4j.Logger;
import org.mango.exception.FluentValidateException;
import java.util.List;

public class FluentValidateCallback extends DefaultValidateCallback implements ValidateCallback {
    Logger logger = Logger.getLogger(FluentValidateCallback.class);
    @Override
    public void onFail(ValidatorElementList validatorElementList, List<ValidationError> errors) {
        StringBuffer sb = new StringBuffer();
        if(null!=errors) {
            for (ValidationError error : errors) {
                sb.append(error.getErrorMsg() + ",");
            }
            String str = sb.toString().substring(0, sb.toString().length() - 1);
            throw new FluentValidateException(str);
        }

    }

    @Override
    public void onSuccess(ValidatorElementList validatorElementList) {
        logger.info("验证通过!");
    }

    @Override
    public void onUncaughtException(Validator validator, Exception e, Object target) throws Exception {
        throw new FluentValidateException(e);
    }
}